package TwoSeach;

import org.junit.Test;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Array
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-11-03  14:42
 * @Description: TODO
 * @Version: 1.0
 */
public class 二分查找704 {

    // int mid=(left+right)>>1

    public int search(int[] nums, int target) {
        if (nums.length == 0) {
            return -1;
        }
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) >> 1;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }

    @Test
    public void test() {
        int search = search(new int[]{-1, 0, 3, 5, 9, 12}, 2);
        System.out.println(search);
    }
}
